package business.global.room.base;

import business.player.Robot.RobotMgr;
import com.ddm.server.common.utils.CommTime;
import jsproto.c2s.cclass.BaseSendMsg;
import org.apache.commons.collections.CollectionUtils;

import java.util.ArrayList;
import java.util.Objects;

/**
 * 安岳一大局位置管理器
 */
public class RobotRoomPosMgr extends AbsRoomPosMgr {

    /**
     * 初始化位置
     *
     * @param room
     */
    public RobotRoomPosMgr(AbsBaseRoom room) {
        super(room);

    }

    @Override
    protected void initPosList() {
        for (int i = 0; i < getPlayerNum(); i++) {
            getAllPosList().add(new RobotRoomPos(i, room));
        }
    }

    /**
     * 获取拥有此牌的玩家位置
     *
     * @param card
     * @return
     */
    public int getPosByCard(Integer card) {
        int pos = -1;
        for (AbsRoomPos roomPosDelegateAbstract : getPosList()) {
            RobotRoomPos roomPos = (RobotRoomPos) roomPosDelegateAbstract;
            if (roomPos.checkCard(card)) {
                pos = roomPos.getPosID();
                break;
            }
        }
        return pos;
    }

    /**
     * 获取所有玩家的手牌
     */
    @SuppressWarnings("unchecked")
    public ArrayList<ArrayList<Integer>> getAllPlayBackNotify() {
        ArrayList<ArrayList<Integer>> cardList = new ArrayList<>();
        for (AbsRoomPos roomPosDelegateAbstract : getPosList()) {
            RobotRoomPos roomPos = (RobotRoomPos) roomPosDelegateAbstract;
            cardList.add((ArrayList<Integer>) roomPos.getPrivateCards().clone());
        }
        return cardList;
    }

    /**
     * 清理开始标识
     */
    public void clearBeginFlag(){
        if (CollectionUtils.isEmpty(this.getPosList())) {
            // 玩家信息列表没数据
            return;
        }
        // 遍历通知所有玩家
        this.getPosList().forEach(key -> {
            if (Objects.nonNull(key)) {
                ((RobotRoomPos)key).setBeginFlag(false);
            }
        });
    }

    /**
     * 检查用户超时
     */
    public void checkOverTime(int ServerTime) {
        if (ServerTime == 0) {
            return;
        }
        for (AbsRoomPos pos : this.getPosList()) {
            if (Objects.isNull(pos) || pos.getPid() <= 0L) {
                continue;
            }
            if (pos.getLatelyOutCardTime() <= 0) {
                continue;
            }
            if (pos.isTrusteeship()) {
                continue;
            }

            if (pos.isRobot() && CommTime.nowMS() > pos.getLatelyOutCardTime() + RobotMgr.getInstance().getThinkTime()) {
                this.getRoom().RobotDeal(pos.getPosID());
                continue;
            }
            if (CommTime.nowMS() > pos.getLatelyOutCardTime() + ServerTime) {
                pos.setLatelyOutCardTime(CommTime.nowMS());
                if (Objects.nonNull(this.getRoom())) {
                    // 启动定时器
                    this.getRoom().startTrusteeShipTime();
                }
                // 增加托管局数
                ((RobotRoomPos)pos).addTuoGuanSetCount();
                pos.setTrusteeship(true, false);
                if (Objects.nonNull(this.getRoom())) {
                    if(room.needAtOnceOpCard()){
                        room.roomTrusteeship(pos.getPosID());
                    }
                }

            }
        }
    }

    @Override
    public void onClose() {
    }

    @Override
    public void notify2All(BaseSendMsg msg) {
        if (CollectionUtils.isEmpty(this.getPosList())) {
            // 玩家信息列表没数据
            return;
        }
        // 遍历通知所有玩家
        this.getPosList().forEach(key -> {
            if (Objects.nonNull(key) && !key.isRobot() && key.getPid() > 0L) {
                key.getPlayer().pushProto(msg);
            }
        });
    }

    @Override
    public void notify2AllPlaying(BaseSendMsg msg) {

    }
}
